package com.meilai.project.mapper.media;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.entity.media.MediaPointChooseDaterangeMediapoint;
import com.meilai.project.entity.media.MediaPointExt;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 选位集合-时间段-具体点位 Mapper 接口
 * </p>
 *
 * @author 张驰
 * @since 2022-01-25
 */
@Mapper
public interface MediaPointChooseDaterangeMediapointMapper extends BaseMapper<MediaPointChooseDaterangeMediapoint> {

    @Delete("delete from media_point_choose_daterange_mediapoint where choose_id = #{chooseId} ")
    int deleteByChooseId(@Param("chooseId") Long chooseId);

    @Select("select mpcdm.daterange_id,mpcdm.media_type_spec_price_id, mp.* " +
            " from media_point_choose_daterange_mediapoint mpcdm " +
            " join media_point mp on mp.id = mpcdm.media_point_id " +
            " where mpcdm.choose_id = #{chooseId} ")
    List<MediaPointExt> listMediaPointByChooseId(@Param("chooseId") Long chooseId);

    @Select("select * from media_point_choose_daterange_mediapoint where choose_id = #{chooseId}")
    List<MediaPointChooseDaterangeMediapoint> listByChooseId(@Param("chooseId") Long chooseId);

    @Insert("insert into media_point_choose_daterange_mediapoint( " +
            "choose_id,daterange_id,media_point_id,media_type_spec_price_id ) " +
            "(select #{new_choose_id},#{new_daterange_id},media_point_id,media_type_spec_price_id" +
            " from media_point_choose_daterange_mediapoint " +
            " where choose_id = #{old_choose_id} and daterange_id = #{old_daterange_id}) ")
    int copyOldToNew(@Param("new_choose_id") Long new_choose_id, @Param("new_daterange_id") Long new_daterange_id,
                     @Param("old_choose_id") Long old_choose_id, @Param("old_daterange_id") Long old_daterange_id);

    @Select("SELECT  " +
            "c.* " +
            "FROM work_order a  " +
            "JOIN bill_sale b ON a.bill_sale_id=b.id " +
            "JOIN media_point_choose_daterange_mediapoint c ON b.choose_id=c.choose_id AND b.daterange_id=c.daterange_id " +
            "WHERE a.id=#{workOrderId} AND c.media_point_id=#{mediaPointId}" +
            " UNION " +
            "SELECT  " +
            "c.* " +
            "FROM work_order a  " +
            "JOIN bill_market_detail b ON a.bill_market_detail_id = b.id " +
            "JOIN media_point_choose_daterange_mediapoint c ON b.choose_id=c.choose_id AND b.daterange_id=c.daterange_id " +
            "WHERE a.id=#{workOrderId} AND c.media_point_id=#{mediaPointId}")
    MediaPointChooseDaterangeMediapoint getChoosePointByWorkOrderIdAndMediaPointId(@Param("workOrderId") Long workOrderId,
                                                                                   @Param("mediaPointId") Long mediaPointId);

    @Select("SELECT  " +
            "c.* " +
            "FROM work_order a  " +
            "JOIN bill_sale b ON a.bill_sale_id=b.id " +
            "JOIN media_point_choose_daterange_mediapoint c ON b.contract_choose_id=c.choose_id AND b.contract_daterange_id=c.daterange_id " +
            "WHERE a.id=#{workOrderId} AND c.media_point_id=#{mediaPointId}")
    MediaPointChooseDaterangeMediapoint getContractChoosePointByWorkOrderIdAndMediaPointId(@Param("workOrderId") Long workOrderId,
                                                                                           @Param("mediaPointId") Long mediaPointId);

    @Select("SELECT " +
            "count(1) AS num " +
            "FROM media_point_choose_daterange_mediapoint mpcdm " +
            "JOIN media_point mp ON mpcdm.media_point_id=mp.id " +
            "WHERE mpcdm.id<>#{id} AND mp.building_area_id=#{building_area_id} AND mpcdm.daterange_id = #{daterange_id} ")
    Integer getOtherPointCount(@Param("id") Long id, @Param("building_area_id") Long building_area_id, @Param("daterange_id") Long daterange_id);
}
